//
//  MovieViewController.m
//  UI14_HomeWork_豆瓣
//
//  Created by Bodi on 16/6/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MovieViewController.h"
#import "MovieModel.h"
#import "MovieTableViewController.h"
#import "ViewController.h"
#import "MovieDetailsViewController.h"

@interface MovieViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *modelArray;


@end

@implementation MovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self config];
    [self handleData];
    [self creatSubView];
}

- (void)config{
    self.navigationItem.title = @"电影";
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.41f green:0.61f blue:0.68f alpha:1.00f];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.modelArray = [[NSMutableArray alloc]init];
    //设置编辑按钮
    self.navigationItem.rightBarButtonItem = [self editButtonItem];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_collection"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
    
}
- (void)rightAction:(UIBarButtonItem *)item{
    ViewController *vc = [[ViewController alloc]init];
   
    [self.navigationController pushViewController:vc animated:YES];

}

- (void)handleData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"movielist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    /* 解析json格式 */
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    NSArray *resultArray = [dictionary objectForKey:@"result"];
    for (NSDictionary *dic in resultArray) {
        MovieModel *model = [[MovieModel alloc]init];
        [model setValuesForKeysWithDictionary:dic];
        [self.modelArray addObject:model];
        [model release];
    }
    
}

- (void)creatSubView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64)];
    [self.view addSubview:_tableView];
    [_tableView release];
    self.tableView.rowHeight = 150;
    self.tableView.separatorColor = [UIColor blackColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    
    [_tableView registerClass:[MovieTableViewController class] forCellReuseIdentifier:@"cell"];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    MovieTableViewController *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    //    Model *model = [self.modelArray objectAtIndex:indexPath.row];
    //    NSURL *url = [NSURL URLWithString:model.pic_url];
    //    NSData *imageData = [NSData dataWithContentsOfURL:url];
    //    UIImage *image = [UIImage imageWithData:imageData];
    //    cell.imageView.image = image;
    //    cell.textLabel.text = model.movieName;
    
    /* 得到对应的model数据 */
    MovieModel *model = [self.modelArray objectAtIndex:indexPath.row];
    /* 给cell的model属性赋值 */
    cell.cellModel = model;
    return cell;
    
    
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieDetailsViewController *modc = [[MovieDetailsViewController alloc]init];
    modc.movieModel = [self.modelArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:modc animated:YES];
    
    
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
